/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.test.example.parser;

import com.test.example.spring.IocBean;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Alexx
 */
public class ParserImpl extends AbstractParser {

    private static List<String> qouteList = new ArrayList<String>();
    private ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");

    @Override
    public String parse() {
        IocBean ioc = (IocBean) context.getBean("file");
        String s = readFile(ioc.getName());
        Pattern p = Pattern.compile("<p>.*?</p>");
        Matcher m = p.matcher(s);
        while (m.find()) {
            String res = m.group();
            res = res.replace("<p>", "");
            res = res.replace("</p>", "");
            qouteList.add(res);
        }
        return getRandomQuote();
    }
    
    private static String getRandomQuote(){
        int randomPosition = (int)(qouteList.size() * Math.random());
        return qouteList.get(randomPosition);
    }

}
